home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pnl010.zip / MDP2.PAS < prev    next >
Pascal/Delphi Source File  |  1992-03-01  |  2KB  |  61 lines

  1. program mdp2;
  2.  
  3. {Program to accompany article in issue #10 of the Pascal NewsLetter.       }
  4. {Author: Mitch Davis, (3:634/384.6) +61-3-890-2062.                        }
  5.  
  6. { From public domain ideas by Ron Lyth, (3:634/384).                       }
  7. {Shows a maximum of 10000 lines from a file specified on the command line. }
  8. {This program works by only allocating as much memory as it needs to store }
  9. {the contents of each line.                                                }
  10.  
  11. {It is limited by two things (which you can see in the While below):       }
  12. { 1) It can only handle as much of the file as it has heap memory to hold, }
  13. { 2) The table of line pointers is limited to about 10000 entries, since   }
  14. {    the table can't be larger than 64k (ie, a little larger than 6*10000) }
  15.  
  16. {$M 16384,80000,655360}
  17.  
  18. uses crt;
  19.  
  20. const MaxLines = 10000;
  21.  
  22. type LineType = record {this takes 6 bytes per line for the pointer}
  23.                   size:word; {2 bytes here.  The size is needed for FreeMem}
  24.                   data:^string; {pointers are 4 bytes}
  25.                 end;
  26.  
  27. var Line:array [1..MaxLines] of LineType; { This takes 60000 bytes }
  28.     Buffer:string;
  29.     LineCount,Loop:word;
  30.     f:text;
  31.  
  32. begin
  33.   writeln ('Reading...');
  34.   assign (f,paramstr (1));
  35.   reset (f);
  36.   LineCount := 0;
  37.   while not (eof (f) or (LineCount = MaxLines) or (MaxAvail < 1024)) do begin
  38.     inc (LineCount);
  39.     write (LineCount,#13);
  40.     readln (f,Buffer);
  41.     with Line [LineCount] do begin
  42.       size := succ(length (Buffer)); {allow for the length byte}
  43.       GetMem (data,size);
  44.       data^ := Buffer;
  45.     end;
  46.   end;
  47.   close (f);
  48.   writeln;
  49.  
  50.   writeln ('Forward:');
  51.   for loop := 1 to LineCount do writeln (loop:3,':',Line [loop].data^);
  52.   writeln;
  53.  
  54.   writeln ('Backwards:');
  55.   for loop := LineCount downto 1 do writeln (loop:3,':',Line [loop].data^);
  56.  
  57.   writeln ('Disposing of memory:');
  58.   for loop := 1 to LineCount do with Line [loop] do FreeMem (data,size);
  59.   writeln ('Done!');
  60. end.
  61.